import QtQuick
import QtQuick.Particles
import QtQuick.Controls

Rectangle {
    width: 600
    height: 400
    color: "black"

    ParticleSystem {
        id: particleSystem
        anchors.fill: parent

        // 红色粒子组
        ItemParticle {
            groups: ["red"]
            delegate: Rectangle {
                width: 16
                height: 16
                radius: 8
                color: "#FF8080"
            }
        }
        // 蓝色粒子组
        ItemParticle {
            groups: ["blue"]
            delegate: Rectangle {
                width: 16
                height: 16
                radius: 8
                color: "#80B0FF"
            }
        }

        // 红色粒子组
        ParticleGroup {
            name: "red"
            duration: 1000
            durationVariation: 500
        }

        // 蓝色粒子组
        ParticleGroup {
            name: "blue"
            duration: 1000
            durationVariation: 500
        }

        // 发射器，发射红色粒子
        Emitter {
            anchors.centerIn: parent
            width: 10
            height: 10
            group: currentGroup
            emitRate: 20
            lifeSpan: 1500
            size: 16
            sizeVariation: 8
            velocity: AngleDirection {
                angle: 0
                angleVariation: 360
                magnitude: 100
            }
        }
    }

    // 说明
    Text {
        anchors.top: parent.top
        anchors.horizontalCenter: parent.horizontalCenter
        color: "white"
        text: "Group切换示例\n"
        horizontalAlignment: Text.AlignHCenter
    }

    // 控制发射粒子组的按钮
    property string currentGroup: "red"
    Row {
        anchors.bottom: parent.bottom
        anchors.horizontalCenter: parent.horizontalCenter
        spacing: 20
        Button {
            id: switchBtn
            text: currentGroup === "red" ? "切换为蓝色粒子" : "切换为红色粒子"
            onClicked: {
                currentGroup = currentGroup === "red" ? "blue" : "red"
            }
        }
    }
}
